package com.agu.web;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.agu.entity.User;
import com.agu.mgt.sys.service.SysUserService;

/**
 * @author Yorke
 */
@RestController
@RequestMapping("/web")
public class ShiroSampleController {

	@Autowired
	SysUserService sysUserService;

	@GetMapping("/login")
	public String login(String username, String password) {
		// System.out.println("==============login===================");
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		// token.setRememberMe(true);
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.login(token);
		return "loginSuccess";
	}

	@GetMapping("/logout")
	public String logout() {
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.logout();
		return "logoutSuccess";
	}

	@GetMapping("/read")
	@RequiresPermissions("read")
	public String read() {
		return "reading";
	}

	@GetMapping("/write")
	@RequiresPermissions("write")
	public String write() {
		return "writing";
	}

	@GetMapping("/success")
	//@RequiresPermissions("admin")
	public Object success() {
		
		Object user =   sysUserService.queryByUserName("admin");
		
		return user;
		
	}

	@GetMapping("/test")
	// @RequiresPermissions("test")
	public String test() {
		return "test";
	}

	@GetMapping("/role")
	@RequiresRoles("role")
	public String role() {
		return "role";
	}
}
